<template>
   <div class="app-container">
     <el-form :model="queryParams" ref="queryForm" :inline="true" >
       <el-form-item label="任务名称" prop="taskName">
         <el-input
           v-model="queryParams.taskName"
           placeholder="请输入任务名称"
           clearable
           style="width: 200px"
           @keyup.enter="handleQuery"
         />
       </el-form-item>
       <el-form-item label="航线名称" prop="fpName">
         <el-input
           v-model="queryParams.fpName"
           placeholder="请输入航线名称"
           clearable
           style="width: 200px"
           @keyup.enter="handleQuery"
         />
       </el-form-item>
       <el-form-item label="执行机场" prop="dockId">
         <el-select v-model="queryParams.dockId" placeholder="请选择执行机场" clearable style="width: 200px">
           <el-option
             v-for="dock in dockOptions"
             :key="dock.id"
             :label="dock.name"
             :value="dock.id"
           />
         </el-select>
       </el-form-item>
       <el-form-item label="任务状态" prop="status">
         <el-select v-model="queryParams.status" placeholder="请选择任务状态" clearable style="width: 200px">
           <el-option
             v-for="dict in task_status"
             :key="dict.value"
             :label="dict.label"
             :value="dict.value"
           />
         </el-select>
       </el-form-item>
       <el-form-item label="执行时间">
         <el-date-picker
           v-model="dateRange"
           type="daterange"
           range-separator="-"
           start-placeholder="开始日期"
           end-placeholder="结束日期"
           value-format="YYYY-MM-DD"
         />
       </el-form-item>
       <el-form-item>
         <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
         <el-button icon="Refresh" @click="resetQuery">重置</el-button>
       </el-form-item>
     </el-form>
 
     <el-row :gutter="10" class="mb8">
       <el-col :span="1.5">
         <el-button
           type="primary"
           plain
           icon="Plus"
           @click="handleAdd"
           v-hasPermi="['uavTask:task:add']"
         >新建任务</el-button>
       </el-col>
     </el-row>
 
     <el-table v-loading="loading" :data="taskList">
       <el-table-column type="selection" width="55" align="center" />
       <el-table-column label="任务名称" align="center" prop="taskName" />
       <el-table-column label="航线名称" align="center" prop="fpName" />
       <el-table-column label="计划执行时间" align="center" prop="execTime">
         <template #default="scope">
           <span>
             {{ 
               scope.row.execType === '2' ? 
               // 周期执行：显示 "每天/每周/每月 HH:mm"
               (scope.row.execCycle === '1' ? '每天 ' : 
                scope.row.execCycle === '2' ? '每周 ' : 
                scope.row.execCycle === '3' ? '每月 ' : '') + 
               (scope.row.cycleTime ? formatCycleTimes(scope.row.cycleTime) : '') :
               // 定时执行或立即执行：显示完整的执行时间列表
               formatExecTimes(scope.row.execTime)
             }}
           </span>
         </template>
       </el-table-column>
       <el-table-column label="执行类型" align="center" prop="execType">
         <template #default="scope">
           <dict-tag :options="task_exec_type" :value="scope.row.execType"/>
         </template>
       </el-table-column>
       <el-table-column label="创建人" align="center" prop="createBy" />
       <el-table-column label="执行机场" align="center" prop="dockName" />
       <el-table-column label="任务状态" align="center" prop="status">
         <template #default="scope">
           <dict-tag :options="task_status" :value="scope.row.status"/>
         </template>
       </el-table-column>
       <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
         <template #default="scope">
           <el-button
             v-if="scope.row.status === '0'"
             type="text"
             icon="Edit"
             @click="handleUpdate(scope.row)"
             v-hasPermi="['uavTask:task:edit']"
           >修改</el-button>
           <el-button
             v-if="scope.row.status === '1'"
             type="text"
             icon="VideoPause"
             @click="handlePause(scope.row)"
             v-hasPermi="['uavTask:task:pause']"
           >暂停</el-button>
           <el-button
             v-if="scope.row.status === '1' || scope.row.status === '3'"
             type="text"
             icon="CircleClose"
             @click="handleStop(scope.row)"
             v-hasPermi="['uavTask:task:stop']"
           >终止</el-button>
           <el-button
             type="text"
             icon="View"
             @click="handleView(scope.row)"
             v-hasPermi="['uavTask:task:query']"
           >查看</el-button>
           <el-button
             v-if="scope.row.status !== '1'"
             type="text"
             icon="Delete"
             @click="handleDelete(scope.row)"
             v-hasPermi="['uavTask:task:remove']"
           >删除</el-button>
         </template>
       </el-table-column>
     </el-table>
     
     <pagination
       v-show="total > 0"
       :total="total"
       :page="queryParams.pageNum"
       :limit="queryParams.pageSize"
       @pagination="handlePagination"
     />
 
     <!-- 添加或修改任务对话框 -->
     <el-dialog :title="title" v-model="open" width="800px" append-to-body>
       <el-form ref="taskFormRef" :model="form" :rules="rules" label-width="120px">
         <el-form-item label="任务名称" prop="taskName">
           <el-input v-model="form.taskName" placeholder="请输入任务名称" />
         </el-form-item>
         <el-form-item label="执行类型" prop="execType">
           <el-select v-model="form.execType" placeholder="请选择执行类型" :disabled="!!form.taskId">
             <el-option
               v-for="dict in task_exec_type"
               :key="dict.value"
               :label="dict.label"
               :value="dict.value"
             />
           </el-select>
         </el-form-item>
         <el-form-item label="执行机场" prop="dockId">
           <el-select v-model="form.dockId" placeholder="请选择执行机场">
             <el-option
               v-for="dock in dockOptions"
               :key="dock.id"
               :label="dock.name"
               :value="dock.id"
             />
           </el-select>
         </el-form-item>
         <el-form-item label="选择航线" prop="fpId">
           <el-select v-model="form.fpId" placeholder="请选择航线">
             <el-option
               v-for="fp in flightPlanOptions"
               :key="fp.fpId"
               :label="fp.fpName"
               :value="fp.fpId"
             />
           </el-select>
         </el-form-item>
         <el-form-item label="返航高度" prop="returnHeight">
           <el-input-number v-model="form.returnHeight" :min="0" :max="500" />
           <span class="ml-2">米</span>
         </el-form-item>
         <el-form-item label="航线失控动作" prop="failAction">
           <el-radio-group v-model="form.failAction">
             <el-radio :value="'0'">退出航线并返航</el-radio>
             <el-radio :value="'1'">继续执行航线任务</el-radio>
           </el-radio-group>
         </el-form-item>
         <el-form-item label="续飞模式" prop="continueMode">
           <el-radio-group v-model="form.continueMode">
             <el-radio :value="'0'">自动续飞</el-radio>
             <el-radio :value="'1'">手动续飞</el-radio>
             <el-radio :value="'2'">不续飞</el-radio>
           </el-radio-group>
         </el-form-item>
         <el-form-item label="AI巡检">
           <el-checkbox-group v-model="aiChecks">
             <el-checkbox :value="'person'">行人</el-checkbox>
             <el-checkbox :value="'vehicle'">车辆</el-checkbox>
             <el-checkbox :value="'fire'">火点</el-checkbox>
             <el-checkbox :value="'smoke'">烟雾</el-checkbox>
           </el-checkbox-group>
         </el-form-item>
         
         <el-form-item v-if="form.execType === '1'" label="执行时间" prop="execTime">
           <div v-for="(time, index) in form.execTime" :key="index" class="time-slot-item">
             <el-date-picker
               v-model="form.execTime[index]"
               type="datetime"
               placeholder="选择执行时间"
             />
             <el-button type="danger" icon="Delete" circle @click="removeTimeSlot('exec', index)" />
           </div>
           <el-button type="primary" icon="Plus" @click="addTimeSlot">添加执行时间</el-button>
         </el-form-item>
         
         <el-form-item v-if="form.execType === '2'" label="执行周期" prop="execCycle">
           <el-select v-model="form.execCycle" placeholder="请选择执行周期" style="width: 120px">
             <el-option
               v-for="dict in task_exec_cycle"
               :key="dict.value"
               :label="dict.label"
               :value="dict.value"
             />
           </el-select>
           <div v-for="(time, index) in form.cycleTime" :key="index" class="time-slot-item">
             <el-time-picker
               v-model="form.cycleTime[index]"
               format="HH:mm"
               placeholder="选择时间"
             />
             <el-button type="danger" icon="Delete" circle @click="removeTimeSlot('cycle', index)" />
           </div>
           <el-button type="primary" icon="Plus" @click="addTimeSlot">添加执行时间</el-button>
         </el-form-item>
         
         <el-form-item v-if="form.execType === '2'" label="默认直播镜头" prop="cameraType">
           <el-radio-group v-model="form.cameraType">
             <el-radio :value="'0'">广角镜头</el-radio>
             <el-radio :value="'1'">变焦镜头</el-radio>
           </el-radio-group>
         </el-form-item>
       </el-form>
       <template #footer>
         <div class="dialog-footer">
           <el-button type="primary" @click="submitForm">确 定</el-button>
           <el-button @click="cancel">取 消</el-button>
         </div>
       </template>
     </el-dialog>
 
     <!-- 任务详情对话框 -->
     <el-dialog title="任务详情" v-model="viewOpen" width="800px" append-to-body>
       <el-descriptions :column="2" border>
         <el-descriptions-item label="任务名称">{{ form.taskName }}</el-descriptions-item>
         <el-descriptions-item label="创建人">{{ form.createBy }}</el-descriptions-item>
         <el-descriptions-item label="任务状态">{{ statusFormat(form) }}</el-descriptions-item>
         <el-descriptions-item label="执行类型">{{ execTypeFormat(form) }}</el-descriptions-item>
         <el-descriptions-item label="执行机场">{{ form.dockName }}</el-descriptions-item>
         <el-descriptions-item label="航线名称">{{ form.fpName }}</el-descriptions-item>
         <el-descriptions-item label="执行时间">
           {{ form.execType === '2' ? 
             (form.execCycle === '1' ? '每天 ' : 
              form.execCycle === '2' ? '每周 ' : 
              form.execCycle === '3' ? '每月 ' : '') + 
             (form.cycleTime ? formatCycleTimes(form.cycleTime) : '') :
             formatExecTimes(form.execTime)
           }}
         </el-descriptions-item>
         <el-descriptions-item label="返航高度">{{ form.returnHeight }}米</el-descriptions-item>
         <el-descriptions-item label="航线失控动作">{{ form.failAction === '0' ? '退出航线并返航' : '继续执行航线任务' }}</el-descriptions-item>
         <el-descriptions-item label="续飞模式">{{ 
           form.continueMode === '0' ? '自动续飞' : 
           form.continueMode === '1' ? '手动续飞' : '不续飞' 
         }}</el-descriptions-item>
         <el-descriptions-item label="AI巡检">
           <el-tag v-if="form.aiPerson === '1'" size="small" style="margin-right: 4px">行人</el-tag>
           <el-tag v-if="form.aiVehicle === '1'" size="small" style="margin-right: 4px">车辆</el-tag>
           <el-tag v-if="form.aiFire === '1'" size="small" style="margin-right: 4px">火点</el-tag>
           <el-tag v-if="form.aiSmoke === '1'" size="small">烟雾</el-tag>
         </el-descriptions-item>
         <el-descriptions-item v-if="form.execType === '2'" label="执行周期">
           <span>
             {{ form.execCycle === '1' ? '每天' : 
                form.execCycle === '2' ? '每周' : 
                form.execCycle === '3' ? '每月' : '' 
             }}
             {{ form.cycleTime ? formatCycleTimes(form.cycleTime) : '' }}
           </span>
         </el-descriptions-item>
         <el-descriptions-item v-if="form.execType === '2'" label="默认直播镜头">
           {{ form.cameraType === '0' ? '广角镜头' : '变焦镜头' }}
         </el-descriptions-item>
       </el-descriptions>

       <div style="margin: 20px 0">
         <el-divider content-position="left">执行记录</el-divider>
       </div>

       <!-- 执行记录表格 -->
       <div v-if="execRecords && execRecords.length > 0">
         <el-table :data="execRecords" style="margin-top: 20px;" v-loading="execRecordLoading">
           <el-table-column label="任务记录ID" align="center" prop="recordId" />
           <el-table-column label="执行时间" align="center" prop="execTime">
             <template #default="scope">
               <span>{{ parseTime(scope.row.execTime) }}</span>
             </template>
           </el-table-column>
           <el-table-column label="执行状态" align="center" prop="status">
             <template #default="scope">
               <el-tag :type="execStatusTypeMap[scope.row.status]">
                 {{ execStatusMap[scope.row.status] }}
               </el-tag>
             </template>
           </el-table-column>
           <el-table-column label="错误信息" align="center" prop="errorMsg" show-overflow-tooltip />
           <el-table-column label="操作人" align="center" prop="createBy" />
         </el-table>

         <!-- 执行记录分页 -->
         <div class="pagination-container" style="margin-top: 15px;">
           <pagination
             v-show="execRecordTotal > 0"
             :total="execRecordTotal"
             :page="execRecordQuery.pageNum"
             :limit="execRecordQuery.pageSize"
             :page-sizes="[5, 10, 20, 50]"
             @pagination="handleExecRecordPagination"
           />
         </div>
       </div>
       <el-empty v-else description="暂无执行记录" />
     </el-dialog>
   </div>
 </template>
 
 <script setup>

 import { ref, onMounted } from 'vue'
 import { useDict } from "@/utils/dict.js";
 import { listTask, getTask, addTask, updateTask, delTask, pauseTask, stopTask, getTaskExecRecords,listDock_info ,listPlans} from '@/api/uavTask/task'
 import { ElMessage, ElMessageBox } from 'element-plus'


 
 const { task_exec_type, task_exec_cycle, task_status } = useDict('task_exec_type', 'task_exec_cycle', 'task_status')

 const loading = ref(false)
 const total = ref(0)
 const taskList = ref([])
 const open = ref(false)
 const viewOpen = ref(false)
 const title = ref('')
 const dockOptions = ref([])
 const flightPlanOptions = ref([])
 const dateRange = ref([])
 const aiChecks = ref([])
 const viewData = ref({})
 const execRecords = ref([])
 const timeList = ref([])
 
 const queryParams = ref({
   pageNum: 1,
   pageSize: 10,
   taskName: undefined,
   fpName: undefined,
   dockId: undefined,
   status: undefined,
   beginTime: undefined,
   endTime: undefined
 })
 
 const form = ref({
   taskId: undefined,
   taskName: undefined,
   fpId: undefined,
   dockId: undefined,
   execType: '0',
   execTime: [],
   execCycle: undefined,
   cycleTime: [],
   returnHeight: 120,
   failAction: '0',
   continueMode: '2',
   cameraType: '0',
   aiPerson: '0',
   aiVehicle: '0',
   aiFire: '0',
   aiSmoke: '0'
 })
 
 const taskFormRef = ref(null)
 
 const rules = {
   taskName: [
     { required: true, message: '任务名称不能为空', trigger: 'blur' }
   ],
   fpId: [
     { required: true, message: '请选择航线', trigger: 'change' }
   ],
   dockId: [
     { required: true, message: '请选择执行机场', trigger: 'change' }
   ],
   execType: [
     { required: true, message: '请选择执行类型', trigger: 'change' }
   ],
   execTime: [
     { 
       required: true, 
       message: '请选择执行时间', 
       trigger: 'change',
       validator: (rule, value, callback) => {
         if (form.value.execType === '1' && !value.length) {
           callback(new Error('请选择执行时间'))
         } else {
           callback()
         }
       }
     }
   ],
   execCycle: [
     { 
       required: true, 
       message: '请选择执行周期', 
       trigger: 'change',
       validator: (rule, value, callback) => {
         if (form.value.execType === '2' && !value) {
           callback(new Error('请选择执行周期'))
         } else {
           callback()
         }
       }
     }
   ],
   cycleTime: [
     { 
       required: true, 
       message: '请选择执行时间', 
       trigger: 'change',
       validator: (rule, value, callback) => {
         if (form.value.execType === '2' && !value.length) {
           callback(new Error('请选择执行时间'))
         } else {
           callback()
         }
       }
     }
   ],
   returnHeight: [
     { required: true, message: '请输入返航高度', trigger: 'blur' }
   ]
 }
 
 // 查询列表
 const getList = () => {
   loading.value = true
   if (dateRange.value && dateRange.value.length === 2) {
     // 设置开始时间为当天的 00:00:00
     const startDate = new Date(dateRange.value[0])
     startDate.setHours(0, 0, 0, 0)
     queryParams.value.beginTime = startDate.toISOString().slice(0, 19).replace('T', ' ')
     
     // 设置结束时间为当天的 23:59:59
     const endDate = new Date(dateRange.value[1])
     endDate.setHours(23, 59, 59, 999)
     queryParams.value.endTime = endDate.toISOString().slice(0, 19).replace('T', ' ')
   }
   listTask(queryParams.value).then(res => {
    console.log(res);
  
     taskList.value = res.rows
     total.value = res.total
     loading.value = false
   })
 }
 
 // 取消按钮
 const cancel = () => {
   open.value = false
   reset()
 }
 
 // 表单重置
 const reset = () => {
   form.value = {
     taskId: undefined,
     taskName: undefined,
     fpId: undefined,
     dockId: undefined,
     execType: '0',
     execTime: [],
     execCycle: undefined,
     cycleTime: [],
     returnHeight: 120,
     failAction: '0',
     continueMode: '2',
     cameraType: '0',
     aiPerson: '0',
     aiVehicle: '0',
     aiFire: '0',
     aiSmoke: '0'
   }
   aiChecks.value = []
   if (taskFormRef.value) {
     taskFormRef.value.resetFields()
   }
 }
 
 // 搜索按钮操作
 const handleQuery = () => {
   queryParams.value.pageNum = 1
   getList()
 }
 
 // 重置按钮操作
 const resetQuery = () => {
   dateRange.value = []
   queryParams.value = {
     pageNum: 1,
     pageSize: 10,
     taskName: undefined,
     fpName: undefined,
     dockId: undefined,
     status: undefined,
     beginTime: undefined,
     endTime: undefined
   }
   handleQuery()
 }
 
 // 新增按钮操作
 const handleAdd = () => {
   reset()
   open.value = true
   title.value = '添加任务'
 }
 
 // 修改按钮操作
 const handleUpdate = (row) => {
   reset()
   const taskId = row.taskId || row.id
   getTask(taskId).then(res => {
     Object.assign(form.value, res.data)
     aiChecks.value = []
     if (res.data.aiPerson === '1') aiChecks.value.push('person')
     if (res.data.aiVehicle === '1') aiChecks.value.push('vehicle')
     if (res.data.aiFire === '1') aiChecks.value.push('fire')
     if (res.data.aiSmoke === '1') aiChecks.value.push('smoke')
     open.value = true
     title.value = '修改任务'
   })
 }
 
 // 提交按钮
 const submitForm = async () => {
   try {
     await taskFormRef.value.validate()
     
     // Format cycleTime for periodic tasks
     if (form.value.execType === '2') {
       form.value.cycleTime = form.value.cycleTime.map(time => {
         if (time instanceof Date) {
           return `${time.getHours().toString().padStart(2, '0')}:${time.getMinutes().toString().padStart(2, '0')}`;
         }
         return time;
       });
     }

     if (form.value.taskId !== undefined) {
       await updateTask(form.value);
       ElMessage.success('修改成功');
     } else {
       await addTask(form.value);
       ElMessage.success('新增成功');
     }
     open.value = false;
     getList();
   } catch (error) {
     console.error('提交表单时发生错误:', error);
     ElMessage.error('提交失败');
   }
 }
 
 // 删除按钮操作
 const handleDelete = (row) => {
   const taskIds = [row.taskId || row.id]
   ElMessageBox.confirm('是否确认删除所选任务?', '警告', {
     confirmButtonText: '确定',
     cancelButtonText: '取消',
     type: 'warning'
   }).then(() => {
     delTask(taskIds).then(() => {
       getList()
       ElMessage.success('删除成功')
     })
   })
 }
 
 // 暂停任务
 const handlePause = (row) => {
   ElMessageBox.confirm('是否确认暂停该任务?', '警告', {
     confirmButtonText: '确定',
     cancelButtonText: '取消',
     type: 'warning'
   }).then(() => {
     pauseTask(row.taskId).then(() => {
       getList()
       ElMessage.success('暂停成功')
     })
   })
 }
 
 // 终止任务
 const handleStop = (row) => {
   ElMessageBox.confirm('是否确认终止该任务?', '警告', {
     confirmButtonText: '确定',
     cancelButtonText: '取消',
     type: 'warning'
   }).then(() => {
     stopTask(row.taskId).then(() => {
       getList()
       ElMessage.success('终止成功')
     })
   })
 }
 
 // 获取机场和航线选项
 const getDockOptions = () => {
   // 模拟API调用
   dockOptions.value = mockDocks
 }
 
 const getFlightPlanOptions = () => {
   // 模拟API调用
   flightPlanOptions.value = mockFlightPlans
 }
 
 // 模拟机场数据
 const mockDocks = [
   { id: 1, name: '机场A', location: '山东省济南市' },
   { id: 2, name: '机场B', location: '山东省青岛市' },
   { id: 3, name: '机场C', location: '山东省烟台市' },
   { id: 4, name: '机场D', location: '山东省潍坊市' }
 ]
 
 // 模拟航线数据
 const mockFlightPlans = [
   { fpId: 46, fpName: '航线A-1', description: '济南市区巡检航线' },
   { fpId: 47, fpName: '航线A-2', description: '济南郊区巡检航线' },
   { fpId: 48, fpName: '航线B-1', description: '青岛市区巡检航线' },
  
 ]
 
 // 格式化执行类型
 const execTypeFormat = (row) => {
   const execType = task_exec_type.value.find(item => item.value === row.execType)
   return execType ? execType.label : ''
 }
 
 // 格式化任务状态
 const statusFormat = (row) => {
   const status = task_status.value.find(item => item.value === row.status)
   return status ? status.label : ''
 }
 
 // 在 script setup 部分添加新的格式化函数
 const formatTime = (time) => {
   if (!time) return '';
   try {
     // 如果是完整的日期时间字符串，根据执行类型显示不同格式
     if (time.includes('T') || time.includes(' ')) {
       const date = new Date(time);
       // 返回完整的日期时间格式 YYYY-MM-DD HH:mm
       return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
     }
     // 如果已经是时分格式，直接返回
     return time;
   } catch (e) {
     console.error('Error formatting time:', e);
     return time;
   }
 }
 
 const formatExecTimes = (times) => {
   if (!times) return '';
   try {
     // 如果是字符串，尝试解析为JSON
     const timeArray = typeof times === 'string' ? JSON.parse(times) : times;
     if (Array.isArray(timeArray)) {
       return timeArray.map(time => {
         if (typeof time === 'string') {
           return formatTime(time);
         }
         return '';
       }).filter(t => t).join(', ');
     }
   } catch (e) {
     console.error('Error parsing execTime:', e);
   }
   return '';
 }
 
 const formatCycleTimes = (times) => {
   if (!times) return '';
   try {
     // 如果是字符串，尝试解析为JSON
     const timeArray = typeof times === 'string' ? JSON.parse(times) : times;
     if (Array.isArray(timeArray)) {
       return timeArray.map(time => {
         if (typeof time === 'string') {
           // 对于周期执行，只显示时和分
           if (time.includes('T') || time.includes(' ')) {
             const date = new Date(time);
             return `${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
           }
           return time;
         }
         return '';
       }).filter(t => t).join(', ');
     }
   } catch (e) {
     console.error('Error parsing cycleTime:', e);
   }
   return '';
 }
 
 // 在 script setup 部分添加相关变量和方法
 const execRecordLoading = ref(false)
 const execRecordTotal = ref(0)
 const execRecordQuery = ref({
   pageNum: 1,
   pageSize: 5,  // 修改默认每页显示数量为5
   taskId: undefined
 })
 
 // 执行状态映射
 const execStatusMap = {
   '0': '开始执行',
   '1': '执行完成',
   '2': '执行失败',
   '3': '任务暂停',
   '4': '任务终止'
 }
 
 // 执行状态对应的标签类型
 const execStatusTypeMap = {
   '0': 'primary',
   '1': 'success',
   '2': 'danger',
   '3': 'warning',
   '4': 'info'
 }
 
 // 获取执行记录
 const getExecRecords = async () => {
   if (!execRecordQuery.value.taskId) return
   execRecordLoading.value = true
   try {
     const response = await getTaskExecRecords(execRecordQuery.value)
     if (response.code === 200) {
       execRecords.value = response.rows
       execRecordTotal.value = response.total
     }
   } catch (error) {
     console.error('获取执行记录失败:', error)
     execRecords.value = []
     execRecordTotal.value = 0
     ElMessage.error('获取执行记录失败')
   } finally {
     execRecordLoading.value = false
   }
 }
 
 // 处理执行记录分页
 const handleExecRecordPagination = (val) => {
   execRecordQuery.value.pageNum = val.page
   execRecordQuery.value.pageSize = val.limit
   getExecRecords()
 }
 
 // 修改查看任务详情方法
 const handleView = async(row) => {
   try {
     const taskId = row.taskId || row.id
     const response = await getTask(taskId)
     form.value = response.data
     viewOpen.value = true
     
     execRecordQuery.value = {
       pageNum: 1,
       pageSize: 5,
       taskId: taskId
     }
     
     await getExecRecords()
   } catch (error) {
     console.error('获取任务详情失败:', error)
     ElMessage.error('获取任务详情失败')
   }
 }
 
 // 添加时间段
 const addTimeSlot = () => {
   if (form.value.execType === '1') {
     form.value.execTime.push(null)
   } else if (form.value.execType === '2') {
     form.value.cycleTime.push(null)
   }
 }
 
 // 删除时间段
 const removeTimeSlot = (type, index) => {
   if (type === 'exec') {
     form.value.execTime.splice(index, 1)
   } else if (type === 'cycle') {
     form.value.cycleTime.splice(index, 1)
   }
 }
 
 // 分页事件处理
 const handlePagination = (e) => {
   queryParams.value.pageNum = e.page
   queryParams.value.pageSize = e.limit
   getList()
 }
 
 onMounted(() => {
   getList()
  //  getDockOptions()
  //  getFlightPlanOptions()
   // 获取机场列表
   listDock_info().then(res => {
    
    console.log(res);
     dockOptions.value = res.rows
   })
   // 获取航线列表
   listPlans().then(res => {
     flightPlanOptions.value = res.rows
   })
 })
 </script>

<style scoped>
.time-slot-item {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
  gap: 10px;
}
</style>